package com.example.test;

import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

import androidx.core.app.NotificationCompat;

public class MyService1 extends Service {
    MediaPlayer player;
    public MyService1() {
    }

    IBinder binder = new LocalBinder();

    @Override
    public void onCreate(){
        super.onCreate();
        if(player == null) {
            player = MediaPlayer.create(this, R.raw.memory); // 音乐文件
        }
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        player.start();
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy(){
        player.stop(); // 停止播放
        player.release();
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
//        throw new UnsupportedOperationException("Not yet implemented");
        return binder;
    }


    public class LocalBinder extends Binder {
        MyService1 getService() {
            return MyService1.this;
        }
    }
}